"""
商品 信息 用例
"""
import math
import unittest
from common.Base import open_browser
from common.databaseTool import DatabaseTool
from Page.goods_info_page import goods_url, GoodsInfoPage


class GoodsInfoCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        # 预置条件 连接数据库
        cls.db = DatabaseTool("168168", "ecshop_db", port=3308)

    def setUp(self):
        # 打开浏览器
        self.driver = open_browser()
        self.lp = GoodsInfoPage(self.driver)
        # 进入指定商品页面
        self.lp.open_url(goods_url)

    def tearDown(self):
        # 关闭浏览器
        self.lp.quit()

    def test_goodsinfo_001(self):  # 验证页面显示的商品名称 是否在数据库中
        try:
            sql = "select goods_name from ecs_goods where goods_name='平衡车'"
            goodname = self.db.fetchOne(sql)["goods_name"]
            self.assertIn(goodname, self.lp.goods_name(), msg="页面显示的商品名称 没在数据库中")
            print(self.db.fetchOne(sql))
        except Exception:
            raise Exception("用例错误")

    def test_goodsinfo_002(self):  # （查询数据库）验证页面显示的商品货号 是否正确
        try:
            sql = "select goods_sn from ecs_goods where goods_name='平衡车'"
            goodsn = self.db.fetchOne(sql)["goods_sn"]
            self.assertIn(goodsn, self.lp.goods_num(), msg="页面显示的商品货号 没在数据库中")
            print(self.db.fetchOne(sql))
        except Exception:
            raise Exception("用例错误")

    def test_goodsinfo_003(self):  # （查询数据库）验证页面显示的商品库存 是否正确
        try:
            sql = "select goods_number from ecs_goods where goods_name='平衡车'"
            goodnumber = str(self.db.fetchOne(sql)["goods_number"])
            self.assertIn(goodnumber, self.lp.goods_stock(), msg="页面显示的商品库存错误")
            print(self.db.fetchOne(sql))
        except Exception:
            raise Exception("用例错误")

    def test_goodsinfo_004(self):  # （查询数据库）验证页面显示的商品品牌 是否正确
        try:
            sql = "select brand_name from ecs_goods join ecs_brand using(brand_id) where  goods_name='平衡车'"
            goodbrand = self.db.fetchOne(sql)["brand_name"]
            self.assertIn(goodbrand, self.lp.goods_brand(), msg="页面显示的商品库存错误")
            print(self.db.fetchOne(sql))
        except Exception:
            raise Exception("用例错误")

    def test_goodsinfo_005(self):  # （查询数据库）验证页面显示的商品重量 是否正确
        try:
            sql = "select goods_weight from ecs_goods where goods_name='平衡车'"
            goodweight = str(int(self.db.fetchOne(sql)["goods_weight"]))
            self.assertIn(goodweight + "克", self.lp.goods_weight(), msg="页面显示的商品库存错误")
            print(self.db.fetchOne(sql))
        except Exception:
            raise Exception("用例错误")

    def test_goodsinfo_006(self):  # （查询数据库）验证页面显示的上架时间 是否正确
        try:
            import time
            sql = "select add_time from ecs_goods where goods_name='平衡车'"  # 获取数据库 商品上架时间戳
            # 将数据库 商品上架时间戳转换为指定格式
            gooduptime = time.strftime("%Y-%m-%d", time.localtime(int(self.db.fetchOne(sql)["add_time"])))
            self.assertIn(gooduptime, self.lp.goods_uptime(), msg="页面显示的商品上架时间错误")
            print(self.lp.goods_uptime())
        except Exception:
            raise Exception("用例错误")

    def test_goodsinfo_007(self):  # （查询数据库）验证页面显示的商品点击次数 是否正确
        try:
            sql = "select click_count from ecs_goods where goods_name='平衡车'"
            goodclick = str(int(self.db.fetchOne(sql)["click_count"]))
            print(goodclick)
            print(self.lp.goods_click_num())
            self.assertIn(goodclick, self.lp.goods_click_num(), msg="页面显示的商品点击次数错误")
        except Exception as e:
            raise Exception("用例错误")

    def test_goodsinfo_008(self):  # （查询数据库） 验证页面显示的商品累计销量 是否正确
        try:
            sql = "select virtual_sales from ecs_goods where goods_name='平衡车'"
            goodsales = str(int(self.db.fetchOne(sql)["virtual_sales"]))
            self.assertIn(goodsales, self.lp.goods_sales(), msg="页面显示的商品累计销量错误")
            print(self.db.fetchOne(sql))
        except Exception:
            raise Exception("用例错误")

    def test_goodsinfo_009(self):  # （查询数据库） 验证页面显示的商品市场价格 是否正确
        try:
            sql = "select market_price from ecs_goods where goods_name='平衡车'"
            # math.ceil 向上取整
            goodmarket = str(int(math.ceil(self.db.fetchOne(sql)["market_price"])))
            self.assertIn(goodmarket, self.lp.goods_market(), msg="页面显示的商品市场价格错误")
            print(self.lp.goods_market())
        except Exception:
            raise Exception("用例错误")

    def test_goodsinfo_010(self):  # （查询数据库） 验证页面显示的商品本店售价 是否正确
        try:
            sql = "select shop_price from ecs_goods where goods_name='平衡车'"
            # math.ceil 向上取整
            goodshop = str(int(math.ceil(self.db.fetchOne(sql)["shop_price"])))
            self.assertIn(goodshop, self.lp.goods_price(), msg="页面显示的商品本店售价错误")
            print(self.lp.goods_price())
        except Exception:
            raise Exception("用例错误")

    def test_goodsinfo_011(self):  # （查询数据库） 验证页面显示的商品注册用户价格 是否正确
        try:
            sql = "select shop_price from ecs_goods where goods_name='平衡车'"
            # math.ceil 向上取整
            goodshop = str(int(math.ceil(self.db.fetchOne(sql)["shop_price"])))
            self.assertIn(goodshop, self.lp.goods_reg_price(), msg="页面显示的商品注册用户价格错误")
            print(self.lp.goods_reg_price())
        except Exception:
            raise Exception("用例错误")

    def test_goodsinfo_011(self):  # （查询数据库） 验证页面显示的商品vip用户价格 是否正确
        try:
            sql = "select shop_price from ecs_goods where goods_name='平衡车'"
            # math.ceil 向上取整
            goodshop = str(int(math.ceil(self.db.fetchOne(sql)["shop_price"])) - 100)
            self.assertIn(goodshop, self.lp.goods_vip_price(), msg="页面显示的商品vip用户价格错误")
            print(self.lp.goods_vip_price())
        except Exception:
            raise Exception("用例错误")

    def test_goodsinfo_012(self):  # （查询数据库） 验证页面显示的商品总价 是否正确
        try:
            sql = "select shop_price from ecs_goods where goods_name='平衡车'"
            # math.ceil 向上取整
            goodshop = str(int(math.ceil(self.db.fetchOne(sql)["shop_price"])))
            self.assertIn(goodshop, self.lp.goods_amount(), msg="页面显示的商品总价错误")
            print(self.lp.goods_amount())
        except Exception:
            raise Exception("用例错误")

    def test_goodsinfo_013(self):  # （查询数据库） 验证页面显示的商品可使用积分 是否正确
        try:
            sql = "select integral from ecs_goods where goods_name='平衡车'"
            goodshop = str(int(self.db.fetchOne(sql)["integral"]))
            self.assertIn(goodshop, self.lp.goods_integral(), msg="页面显示的商品可使用积分错误")
            print(self.lp.goods_integral())
        except Exception:
            raise Exception("用例错误")


if __name__ == '__main__':
    unittest.main()
